home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cserial.zip / TIMER.C < prev    next >
C/C++ Source or Header  |  1990-04-04  |  3KB  |  188 lines

  1. /*
  2.  *                               TIMER.C
  3.  *
  4.  *                           Written for the
  5.  *
  6.  *                              Datalight
  7.  *                           Microsoft V 5.x
  8.  *                                TurboC
  9.  *                                  &
  10.  *                               Zortech
  11.  *
  12.  *                             C Compilers
  13.  *
  14.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  15.  *
  16.  *    The timer routines used here are little approximations
  17.  *    since the Timer Interrupt ( 0x1C ) is only updated 18
  18.  *  times a second.
  19.  *
  20.  *    The routines are NOT bulletproof - if you decide to use
  21.  *    them I would heartily suggest you include a 
  22.  *    Control Break (Interrupt 0x23) intercept routine
  23.  *    to allow for cleanup before Going South...
  24.  *
  25.  *    There are basically three routines which you call
  26.  *        timer_init () - sets up the timer interrupt vector
  27.  *                        (the interrupt handler is imbedded in
  28.  *                         there somewhere).
  29.  *
  30.  *        timer_set () -  sets the timer counter to 0
  31.  *
  32.  *        timer_read () - returns the current value of the timer
  33.  *                        counter.
  34.  *
  35.  *        timer_term () - a 'Must Do' - clean up our act and give
  36.  *                        the interrupt vector back
  37.  */
  38. #include "dependnt.h"
  39.  
  40. #if (!defined (TRUE))
  41. #    define TRUE  (1)
  42. #    define FALSE (0)
  43. #endif
  44.  
  45. volatile unsigned long int _Timer_cnt;
  46. static int _Timer_Active = 0;
  47.  
  48. #if (defined (DLC))
  49. int 
  50. Timer_ISR ()
  51. {
  52.     _Timer_cnt++;
  53.     return (0);
  54. }
  55. #else
  56. void    (interrupt far * timer_save_vec) (void);
  57. void interrupt far 
  58. Timer_ISR (void)
  59. {
  60.     _Timer_cnt++;
  61.     (*timer_save_vec) ();
  62. }
  63. #endif
  64.  
  65.  
  66.  
  67. /*
  68.  *    TIMER_INIT
  69.  */
  70.  
  71. int 
  72. timer_init ()
  73. {
  74.     if (!_Timer_Active)
  75.     {
  76. #if (defined (DLC))
  77.         int_intercept (0x1C, &Timer_ISR, 128);
  78. #else
  79.         timer_save_vec = getvect (0x1c);
  80.         setvect (0x1C, Timer_ISR);
  81. #endif
  82.         _Timer_Active = TRUE;
  83.     }
  84. }
  85.  
  86.  
  87.  
  88. /*
  89.  *    TIMER_TERM
  90.  */
  91.  
  92. int 
  93. timer_term ()
  94. {
  95.     if (_Timer_Active)
  96.     {
  97. #if (defined (DLC))
  98.         int_restore (0x1C);
  99. #else
  100.         setvect (0x1C, timer_save_vec);
  101. #endif
  102.         _Timer_Active = FALSE;
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. /*
  109.  *    TIMER_SET
  110.  */
  111.  
  112. int 
  113. timer_set ()
  114. {
  115.     _Timer_cnt = 0L;
  116. }
  117.  
  118.  
  119.  
  120. /*
  121.  *    TIMER_READ
  122.  */
  123.  
  124. unsigned long int 
  125. timer_read ()
  126. {
  127.     return (_Timer_cnt);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. /*
  134.  *    TIMEOUT - go away for the specified number of seconds
  135.  */
  136.  
  137. int 
  138. timeout (sec)
  139. unsigned int sec;
  140. {
  141.     unsigned long int flag;
  142.     sec *= 18;
  143.     _Timer_cnt = 0L;
  144.     do
  145.     {
  146.         flag = _Timer_cnt;
  147.     } while (flag < sec);
  148. }
  149.  
  150. static unsigned int DELAY_millisecond;
  151. void
  152. DELAY_init (void)
  153. {
  154.     static int d_init = 0;
  155.     int        tna = 0;
  156.     long    l;
  157.     if (d_init)
  158.         return;
  159.     d_init = 1;
  160.     if (!_Timer_Active)
  161.     {
  162.         timer_init ();
  163.         tna = 1;
  164.     }
  165.     timer_init ();
  166.     timer_set ();
  167.     while (timer_read () == 0L)
  168.         ;
  169.     timer_set ();
  170.     for (l = 0L; l != -1L; l++)
  171.         if (timer_read () == 3)
  172.             break;
  173.     l *= 197;
  174.     DELAY_millisecond = (int) ((l + 5000) / 10000);
  175.     if (tna)
  176.         timer_term ();
  177. }
  178. static unsigned int _DELAY_cnt, _DELAY_ix;
  179.  
  180. int 
  181. DELAY_loop (int msc)
  182. {
  183.     for (_DELAY_ix = 0; _DELAY_ix < msc; _DELAY_ix++)
  184.         for (_DELAY_cnt = 0; _DELAY_cnt < DELAY_millisecond; _DELAY_cnt++)
  185.             ;
  186.     return (msc);
  187. }
  188.